package com.ruoyi.web.controller.system;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.JingfeiYue;
import com.ruoyi.system.domain.ZhuanjiaBiandongDetail;
import com.ruoyi.system.service.IJingfeiYueService;
import com.ruoyi.web.controller.tool.JsonResult;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 余额查看Controller
 * 
 * @author ruoyi
 * @date 2023-04-11
 */
@Controller
@RequestMapping("/system/yue")
public class JingfeiYueController extends BaseController
{
    private String prefix = "system/yue";

    @Autowired
    private IJingfeiYueService jingfeiYueService;

    @RequiresPermissions("system:yue:view")
    @GetMapping()
    public String yue()
    {
        return prefix + "/yue";
    }

    @GetMapping("/customyue")
    public String custom_yue()
    {
        return prefix + "/custom_yue";
    }

    @GetMapping("/customlist")
    public String customlist(@RequestParam(value = "parentid", required = false) String parentid, ModelMap ppMap)
    {
        ppMap.put("parentid",parentid);
        return prefix + "/customlist";
    }

    /**
     * 查询余额查看列表
     */
    @RequiresPermissions("system:yue:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(JingfeiYue jingfeiYue)
    {
        startPage();
        List<JingfeiYue> list = jingfeiYueService.selectJingfeiYueList(jingfeiYue);
        return getDataTable(list);
    }

    /**
     * 导出余额查看列表
     */
    @RequiresPermissions("system:yue:export")
    @Log(title = "余额查看", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(JingfeiYue jingfeiYue)
    {
        List<JingfeiYue> list = jingfeiYueService.selectJingfeiYueList(jingfeiYue);
        ExcelUtil<JingfeiYue> util = new ExcelUtil<JingfeiYue>(JingfeiYue.class);
        return util.exportExcel(list, "余额查看数据");
    }

    /**
     * 新增余额查看
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存余额查看
     */
    @RequiresPermissions("system:yue:add")
    @Log(title = "余额查看", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(JingfeiYue jingfeiYue)
    {
        return toAjax(jingfeiYueService.insertJingfeiYue(jingfeiYue));
    }

    /**
     * 修改余额查看
     */
    @RequiresPermissions("system:yue:edit")
    @GetMapping("/edit/{deptid}")
    public String edit(@PathVariable("deptid") String deptid, ModelMap mmap)
    {
        JingfeiYue jingfeiYue = jingfeiYueService.selectJingfeiYueByDeptid(deptid);
        mmap.put("jingfeiYue", jingfeiYue);
        return prefix + "/edit";
    }

    /**
     * 修改保存余额查看
     */
    @RequiresPermissions("system:yue:edit")
    @Log(title = "余额查看", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(JingfeiYue jingfeiYue)
    {
        return toAjax(jingfeiYueService.updateJingfeiYue(jingfeiYue));
    }

    /**
     * 删除余额查看
     */
    @RequiresPermissions("system:yue:remove")
    @Log(title = "余额查看", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(jingfeiYueService.deleteJingfeiYueByDeptids(ids));
    }

    @PostMapping( "/find_one")
    @ResponseBody
    public JsonResult<?> findOne(@RequestParam("deptid") String ppDeptId)
    {
        try{
            JingfeiYue mmJingfeiyue = jingfeiYueService.selectJingfeiYueByDeptid(ppDeptId);
            return mmJingfeiyue==null?JsonResult.getErrorResult("单位余额还未生成"):JsonResult.getSuccessResult(mmJingfeiyue);
        }catch (Exception e){
            return JsonResult.getErrorResult(e.getMessage());
        }
    }

    /**
     * 查询 余额管理列表
     */
    @PostMapping("/yue")
    @ResponseBody
    public JsonResult<?> yue(@RequestParam("deptid")String ppDeptId)
    {
        try{
            JingfeiYue jingfeiYue = jingfeiYueService.selectYue(ppDeptId);
            return JsonResult.getSuccessResult(jingfeiYue);
        } catch (Exception e) {
            return JsonResult.getErrorResult("查询余额失败");
        }
    }

    //上级单位查询下一级单位余额情况
    @PostMapping("/find_by_parentid")
    @ResponseBody
    public JsonResult<?> findByParentId(@RequestParam("parentid")String ppParentId)
    {
        try{
            List<Map<String,Object>> mmList = jingfeiYueService.findByParentId(ppParentId);
            return JsonResult.getSuccessResult(mmList);
        } catch (Exception e) {
            return JsonResult.getErrorResult("查询下级单位余额失败");
        }
    }
}
